package models;

import java.util.*;

import javax.persistence.*;

import play.data.validation.Required;
import play.db.jpa.*;

@Entity
public class Candidat extends Model {

	@ManyToMany(cascade=CascadeType.ALL)
	public List<Uv> examenUv;

	public boolean candidPartielle=false;
	
	@ManyToOne
	public Resultat resultat;

	@ManyToOne
	@Required
	public Agent agent;

	@ManyToOne
	public Stage stage;

	public Agent getAgent() {
		return agent;
	}

	@ManyToOne
	public Intitule intitule;

	@ManyToOne
	public EtatCandid etatCandid;

	public boolean demandeAnnulation;

	public boolean logement=false;

	@Required
	public String motivation;

	public String precisionDate;

	public String motifRefus;

	@ManyToOne
	public MotifAnnulation motifAnnulation;

	public boolean candidature_non_annuler=false;

	@Required
	@ManyToOne
	public Role role;

	@ManyToOne
	public OrdresPriorite ordrePrioriteCis;
	
	@ManyToOne
	public OrdresPriorite ordrePrioriteGrpmt;

	public Candidat(Agent agent, Stage stage, String motivation,Long role, Intitule intitule){
		this.agent = agent;
		this.stage = stage;
		this.motivation=motivation;
		this.role = Role.findById(role);
		etatCandid = (EtatCandid) EtatCandid.findAll().get(0);	
		this.intitule=intitule;
		this.examenUv=new ArrayList<Uv>();
	}

	public void candidater(boolean logement, Long role, String precisionDate, boolean candidPartielle){
		this.agent.addCandidature(this);
		this.stage.addCandidature(this);
		Role roles = Role.findById(role);
		this.role.setNom(roles.getNom());
		this.logement=logement;
		this.precisionDate=precisionDate;
		this.candidPartielle=candidPartielle;
	}

	public void candidaterNonPlanifiee(boolean logement, Long role, String precisionDate, Long intitule, boolean candidPartielle){
		this.agent.addCandidature(this);
		Role roles = Role.findById(role);
		Intitule intituleObj=Intitule.findById(intitule);
		this.role.setNom(roles.getNom());
		this.logement=logement;
		this.precisionDate=precisionDate;	
		this.intitule=intituleObj;
		this.candidPartielle=candidPartielle;
	}

	public String getMotifRefus() {
		return motifRefus;
	}

	public void setMotifRefus(String motifRefus) {
		this.motifRefus = motifRefus;
	}

	public boolean equals(Candidat c){
		return c.agent.equals(this.agent)&&c.stage.equals(this.stage);

	}

	public String toString(){
		return agent.nom+" "+agent.prenom;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}

	public EtatCandid getEtatCandid() {
		return etatCandid;
	}

	public void setEtatCandid(EtatCandid etatCandid) {
		this.etatCandid = etatCandid;
	}

	public MotifAnnulation getMotifAnnulation() {
		return motifAnnulation;
	}

	public void setMotifAnnulation(MotifAnnulation motifAnnulation) {
		this.motifAnnulation = motifAnnulation;
	}

}
